package cn.edu.zime.tjh;

import cn.edu.zime.tjh.gui.ClientGUIApp;
import cn.edu.zime.tjh.gui.ServerGUIApp;
import cn.edu.zime.tjh.gui.UnifiedGUIApp;
import cn.edu.zime.tjh.server.EchoServer;
import cn.edu.zime.tjh.server.EchoUDPServer;
import cn.edu.zime.tjh.client.EchoClient;
import cn.edu.zime.tjh.client.EchoUDPClient;
import javafx.application.Application;

import java.util.Scanner;

public class MainLauncher {
    public static void main(String[] args) {
        if (args.length > 0) {
            String mode = args[0].toLowerCase();
            switch (mode) {
                case "server-gui":
                    System.out.println("正在启动服务器GUI...");
                    ServerGUIApp.main(args);
                    break;
                case "client-gui":
                    System.out.println("正在启动客户端GUI...");
                    ClientGUIApp.main(args);
                    break;
                case "server":
                case "tcp-server":
                    System.out.println("正在启动TCP控制台服务器...");
                    EchoServer.getInstance().start();
                    break;
                case "client":
                case "tcp-client":
                    System.out.println("正在启动TCP控制台客户端...");
                    EchoClient client = new EchoClient();
                    client.start();
                    break;
                case "udp-server":
                    System.out.println("正在启动UDP控制台服务器...");
                    EchoUDPServer.getInstance().start();
                    break;
                case "udp-client":
                    System.out.println("正在启动UDP控制台客户端...");
                    EchoUDPClient udpClient = new EchoUDPClient();
                    udpClient.start();
                    break;
                default:
                    showUsage();
            }
        } else {
            showInteractiveMenu();
        }
    }
    
    private static void showUsage() {
        System.out.println("用法: java MainLauncher [模式]");
        System.out.println("模式:");
        System.out.println("  server-gui   - 启动服务器GUI");
        System.out.println("  client-gui   - 启动客户端GUI");
        System.out.println("  server       - 启动TCP控制台服务器");
        System.out.println("  tcp-server   - 启动TCP控制台服务器");
        System.out.println("  client       - 启动TCP控制台客户端");
        System.out.println("  tcp-client   - 启动TCP控制台客户端");
        System.out.println("  udp-server   - 启动UDP控制台服务器");
        System.out.println("  udp-client   - 启动UDP控制台客户端");
        System.out.println("  (无参数)      - 交互模式");
    }
    
    private static void showInteractiveMenu() {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("IoT通信系统启动器");
        System.out.println("1. 启动统一GUI管理器 (推荐)");
        System.out.println("2. 启动服务器 (GUI模式)");
        System.out.println("3. 启动客户端 (GUI模式)");
        System.out.println("4. 启动TCP服务器 (控制台模式)");
        System.out.println("5. 启动TCP客户端 (控制台模式)");
        System.out.println("6. 启动UDP服务器 (控制台模式)");
        System.out.println("7. 启动UDP客户端 (控制台模式)");
        System.out.println("8. 退出");
        System.out.print("请选择 (1-8): ");
        
        try {
            int choice = scanner.nextInt();
            
            switch (choice) {
                case 1:
                    System.out.println("正在启动统一GUI管理器...");
                    UnifiedGUIApp.main(new String[0]);
                    break;
                case 2:
                    System.out.println("正在启动服务器GUI...");
                    ServerGUIApp.main(new String[0]);
                    break;
                case 3:
                    System.out.println("正在启动客户端GUI...");
                    ClientGUIApp.main(new String[0]);
                    break;
                case 4:
                    System.out.println("正在启动TCP服务器控制台模式...");
                    startConsoleTCPServer();
                    break;
                case 5:
                    System.out.println("正在启动TCP客户端控制台模式...");
                    startConsoleTCPClient();
                    break;
                case 6:
                    System.out.println("正在启动UDP服务器控制台模式...");
                    startConsoleUDPServer();
                    break;
                case 7:
                    System.out.println("正在启动UDP客户端控制台模式...");
                    startConsoleUDPClient();
                    break;
                case 8:
                    System.out.println("再见!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效选择，请重试。");
            }
        } catch (Exception e) {
            System.out.println("无效输入，请重试。");
        } finally {
            scanner.close();
        }
    }
    
    private static void startConsoleTCPServer() {
        try {
            EchoServer.getInstance().start();
        } catch (Exception e) {
            System.err.println("启动TCP服务器失败: " + e.getMessage());
        }
    }
    
    private static void startConsoleTCPClient() {
        try {
            EchoClient client = new EchoClient();
            client.start();
        } catch (Exception e) {
            System.err.println("启动TCP客户端失败: " + e.getMessage());
        }
    }
    
    private static void startConsoleUDPServer() {
        try {
            EchoUDPServer.getInstance().start();
        } catch (Exception e) {
            System.err.println("启动UDP服务器失败: " + e.getMessage());
        }
    }
    
    private static void startConsoleUDPClient() {
        try {
            EchoUDPClient client = new EchoUDPClient();
            client.start();
        } catch (Exception e) {
            System.err.println("启动UDP客户端失败: " + e.getMessage());
        }
    }
}